package com.ruoyi.bpm.service.category.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.ruoyi.bpm.controller.admin.definition.vo.category.BpmCategoryPageReqVO;
import com.ruoyi.bpm.controller.admin.definition.vo.category.BpmCategorySaveReqVO;
import com.ruoyi.bpm.domain.category.BpmCategory;
import com.ruoyi.bpm.mapper.category.BpmCategoryMapper;
import com.ruoyi.bpm.service.category.BpmCategoryService;
import com.ruoyi.bpm.utils.BeanUtils;
import com.ruoyi.common.orm.core.page.PageQuery;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static com.ruoyi.bpm.domain.category.table.BpmCategoryTableDef.BPM_CATEGORY;
import static com.ruoyi.bpm.enums.ErrorCodeConstants.*;
import static com.ruoyi.common.core.utils.ServiceExceptionUtil.exception;

/**
 * BPM 流程分类 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BpmCategoryServiceImpl extends BaseServiceImpl<BpmCategoryMapper, BpmCategory> implements BpmCategoryService {
    @Override
    public QueryWrapper query() {
        return super.query().from(BPM_CATEGORY);
    }

    @Override
    public Long createCategory(BpmCategorySaveReqVO createReqVO) {
        // 校验唯一
        validateCategoryNameUnique(createReqVO);
        validateCategoryCodeUnique(createReqVO);
        // 插入
        BpmCategory category = BeanUtils.toBean(createReqVO, BpmCategory.class);
        super.save(category);
        return category.getId();
    }

    @Override
    public void updateCategory(BpmCategorySaveReqVO updateReqVO) {
        // 校验存在
        validateCategoryExists(updateReqVO.getId());
        validateCategoryNameUnique(updateReqVO);
        validateCategoryCodeUnique(updateReqVO);
        // 更新
        BpmCategory updateObj = BeanUtils.toBean(updateReqVO, BpmCategory.class);
        super.updateById(updateObj);
    }

    private void validateCategoryNameUnique(BpmCategorySaveReqVO updateReqVO) {
        BpmCategory category = super.getOne(query().where(BPM_CATEGORY.NAME.eq(updateReqVO.getName())));
        if (category == null
            || ObjUtil.equal(category.getId(), updateReqVO.getId())) {
            return;
        }
        throw exception(CATEGORY_NAME_DUPLICATE, updateReqVO.getName());
    }

    private void validateCategoryCodeUnique(BpmCategorySaveReqVO updateReqVO) {
        BpmCategory category = super.getOne(query().where(BPM_CATEGORY.CODE.eq(updateReqVO.getCode())));
        if (category == null
            || ObjUtil.equal(category.getId(), updateReqVO.getId())) {
            return;
        }
        throw exception(CATEGORY_CODE_DUPLICATE, updateReqVO.getCode());
    }

    @Override
    public void deleteCategory(Long id) {
        // 校验存在
        validateCategoryExists(id);
        // 删除
        this.mapper.deleteById(id);
    }

    private void validateCategoryExists(Long id) {
        if (this.getCategory(id) == null) {
            throw exception(CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public BpmCategory getCategory(Long id) {
        return super.mapper.selectOneById(id);
    }

    @Override
    public TableDataInfo<BpmCategory> getCategoryPage(BpmCategoryPageReqVO pageReqVO) {
        QueryWrapper queryWrapper = query()
            .where(BPM_CATEGORY.NAME.like(pageReqVO.getName(), StrUtil.isNotBlank(pageReqVO.getName())))
            .and(BPM_CATEGORY.CODE.like(pageReqVO.getCode(), StrUtil.isNotBlank(pageReqVO.getCode())))
            .and(BPM_CATEGORY.STATUS.eq(pageReqVO.getStatus(), pageReqVO.getStatus() != null))
            .and(BPM_CATEGORY.CREATE_TIME.between(pageReqVO.getCreateTime(), ArrayUtil.isNotEmpty(pageReqVO.getCreateTime())))
            .orderBy(BPM_CATEGORY.SORT.asc());
        Page<BpmCategory> page = this.pageAs(PageQuery.build(), queryWrapper, BpmCategory.class);
        return TableDataInfo.build(page);
    }

    @Override
    public List<BpmCategory> getCategoryListByCode(Collection<String> codes) {
        if (CollUtil.isEmpty(codes)) {
            return Collections.emptyList();
        }
        return super.list(query().where(BPM_CATEGORY.CODE.in(codes)));
    }

    @Override
    public List<BpmCategory> getCategoryListByStatus(Integer status) {
        return super.list(query().where(BPM_CATEGORY.STATUS.eq(status)));
    }

}
